<?php

//Created by Joanna Tan

class result_model extends CI_Model {

    var $setnumber = '';
    var $numrecords = '';
    var $numentries = '';
    var $keyword = '';
    var $id = "";
    var $setentry = "";
    var $docnum = "";
    var $bktitle = "";
    var $author = "";
    var $img = "";
    var $callnumber = "";
    var $year = "";
    var $bkformat = "";
    var $ebooklink = "";

    function __construct() {
        parent::__construct();
    }

 /*
|--------------------------------------------------------------------------
| sortXml (@author : Joanna Tan)
|--------------------------------------------------------------------------
|
| This function parses the Sort XML
| 
|
*/ 
    function sortXml($sorterXml) {
        $tryarray = array();
        $sortoutput = simplexml_load_file($sorterXml);
        foreach ($sortoutput->children() as $child) {
            
        }
    }


 /*
|--------------------------------------------------------------------------
| praseFirstXml (@author : Joanna Tan)
|--------------------------------------------------------------------------
|
| This function parses the Search XML. It will return the set number
| numbers of entries and number of records
|
*/ 
    function praseFirstXml($xmlOne, $keyword) {
        $resultOne = array();
        $setnumber = "";
        $numrecords = "";
        $numentries = "";
        $currentstart = 1;
        $currentend = 10;
        $currentpagenumber = 1;
        $resultOneData = simplexml_load_file($xmlOne);
        $resultInfoList = array();

        foreach ($resultOneData->children() as $child) {
            $setnumber = $resultOneData->set_number;
            $numrecords = $resultOneData->no_records;
            $numentries = $resultOneData->no_entries;
        }

        if ($numentries < 10) {
            $currentend = $numentries;
        }
        $item = new Result_Model();
        $item->setnumber = $setnumber;
        $item->numrecords = $numrecords;
        $item->numentries = $numentries;
        $item->keyword = $keyword;
        $item->currentstart = $currentstart;
        $item->currentend = $currentend;
        $item->currentpagenumber = $currentpagenumber;

        $resultOne = array('setnumber' => (int) $item->setnumber,
            'numrecords' => (int) $item->numrecords,
            'numentries' => (int) $item->numentries,
            'keyword' => $item->keyword,
            'currentstart' => (int) $item->currentstart,
            'currentend' => (int) $item->currentend,
            'currentpagenumber' => (int) $item->currentpagenumber);

        $resultInfoList = array('resultsInfo' => $resultOne);
        return $resultInfoList;
    }


 /*
|--------------------------------------------------------------------------
| praseSecondXml (@author : Joanna Tan)
|--------------------------------------------------------------------------
|
| This function parses the Results XML. It will return the data that
| is required to be display in the result list. 
|
*/ 
    function praseSecondXml($xmlTwo, $part2) {
        $resultTwoData = simplexml_load_file($xmlTwo);
        $resultTwo = array();
        $finList = array();
        $img1Url = $this->config->item('img1Url');
        $img2Url = $this->config->item('img2Url');

        foreach ($resultTwoData->children() as $child) {
            $count = 0;
            $fauthorCom = "";
            $ftitleCom = "";
            $fcallnumber = "";
            $fformat = "";
            $fsetentry = "";
            $fdocnum = "";
            $fyear = "";
            $fisbn = "";
            $space = " ";
            $ebooklinki = '';

            if ($child->getName() == "record") {

                foreach ($child->children() as $docn) {
                    if ($docn->getName() == "doc_number") {
                        $fdocnum = $docn;
                    }

                    if ($docn->getName() == "record_header") {
                        foreach ($docn->children() as $sentry) {
                            $fsetentry = $sentry;
                        }
                    }

                    if ($docn->getName() == "metadata") {
                        foreach ($docn->children() as $oai) {
                            foreach ($oai->children() as $fixfieldo) {
                                if ($fixfieldo->attributes() == "FMT") {
                                    $fformat = $fixfieldo;
                                }
                            }
                        }
                    }

                    foreach ($docn->children() as $setentry) {
                        foreach ($setentry->children() as $data) {
                            foreach ($data->children() as $display => $next) {
                                if ($data->attributes() == "100") {
                                    if (empty($fauthorCom)) {
                                        $fauthorCom = $next;
                                    } else {
                                        $fauthorCom = $fauthorCom . $next;
                                    }
                                }

                                if ($data->attributes() == "245") {
                                    $count = $count + 1;
                                    if (empty($ftitleCom)) {
                                        $ftitleCom = $next;
                                    } else {
                                        $ftitleCom = $ftitleCom . $space . $next;
                                    }
                                }

                                if ($data->attributes() == "099") {
                                    $fcallnumber = $next;
                                }

                                if ($data->attributes() == "020") {

                                    foreach ($data->children() as $isb) {
                                        foreach ($isb->attributes() as $isbnatt) {
                                            if ($isbnatt == "a") {
                                                $isbnprocess = (string) preg_replace("/[^0-9]/", "", $isb);
                                                if (strlen($isbnprocess) == '13') {
                                                    $fisbnLength = strlen($fisbn);
                                                    if ($fisbn == "" || $fisbnLength <= '10') {
                                                        $fisbn = $isbnprocess;
                                                    }
                                                } else {
                                                    if ($fisbn == "") {
                                                        $fisbn = $isbnprocess;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }

                                if ($data->attributes() == "260") {
                                    foreach ($data->children() as $try) {
                                        foreach ($try->attributes() as $att) {
                                            if ($att == "c") {
                                                $fyear = (string) preg_replace("/[^0-9]/", "", $try);
                                                $fyear = substr($fyear, 0, 4);
                                            }
                                        }
                                    }
                                }

                                if ($data->attributes() == "856") {
                                    foreach ($data->children() as $subfield) {
                                        if ($subfield->attributes() == "u") {
                                            $ebooklinki = (string) $subfield;
                                           // $fformat = 'eBook';
                                        }
                                    }
                                }
                                
                                  if ($data->attributes() == "591") {
                                    foreach ($data->children() as $subfieldfor) {
                                        if ($subfieldfor->attributes() == "a") {
                                            $fformat = $subfieldfor;
                                        }
                                    }
                                }
                                
                            }
                        }
                    }
                }
                $item = new Result_Model();
                $item->setentry = $fsetentry;
                $item->docnum = $fdocnum;
                $item->bktitle = $ftitleCom;
                $item->author = $fauthorCom;
                if ($fisbn == '') {
                    $item->img = '';
                } else {
                    $item->img = $img1Url . $fisbn . $img2Url;
                }
                $item->callnumber = $fcallnumber;
                $item->year = $fyear;
                $item->bkformat = $fformat;
                $item->ebooklink = $ebooklinki;
                $resultTwo[] = array('setentry' => (int) $item->setentry, 'docnum' => (string) $item->docnum,
                    'bktitle' => (string) $item->bktitle, 'author' => (string) $item->author, 'img' => (string) $item->img,
                    'callnumber' => (string) $item->callnumber, 'year' => (string) $item->year,
                    'bkformat' => (string) $item->bkformat, 'ebooklink' => (string) $item->ebooklink);
            }
        }
        $finList = array('result' => $resultTwo);
        return $finList;
    }
	
	


}

?>
